// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    
    @property(cc.Node)
    cube: cc.Node = null;

    @property(cc.Node)
    high: cc.Node = null;

    @property(cc.Node)
    low: cc.Node = null;

    _data: any = null;

    //开盘价 收盘价 最高价 最低价 价格比例对比值(1像素等于多少分)
    //水平线价格等于开盘价
    setItem (data, rate, nodeWidth, key) {
        let open = data['open' + key];
        let close = data['close' + key];
        let high = data['high' + key];
        let low = data['low' + key];
        this._data = data;
        let inv = Math.abs(close - open);
        this.cube.height = inv * rate;
        
        if (this.cube.width > nodeWidth - 2) {
            this.cube.width = nodeWidth - 2;
        }
        this.node.width = this.cube.width;
        if (this.cube.height < 2) {
            this.cube.height = 2;
        }
        if (close >= open) {
            this.cube.scaleY = 1;
            this.setColor(cc.color(180, 0, 0));
        } else {
            this.cube.scaleY = -1;
            this.setColor(cc.color(0, 150, 0));
        }
        
        this.high.height = rate * Math.abs(high - open);
        this.low.height = rate * Math.abs(low - open);
    }

    setColor (color: cc.Color) {
        this.cube.color = color;
        this.low.color = color;
        this.high.color = color;
    }

    onClick () {
        if (this.node.opacity > 0) {
            cc.director.emit('ClickKLineNode', this._data);
        }
    }
}
